home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Atari Mega Archive 1
/
Atari Mega Archive - Volume 1.iso
/
games
/
larn12s.arc
/
LARN.ARC
/
IO.C
< prev
next >
Wrap
C/C++ Source or Header
|
1987-10-28
|
25KB
|
1,028 lines
/* io.c Larn is copyrighted 1986 by Noah Morgan.
*
* Below are the functions in this file:
*
* setupvt100() Subroutine to set up terminal in correct mode for game
* clearvt100() Subroutine to clean up terminal when the game is over
* getchar() Routine to read in one character from the terminal
* scbr() Function to set cbreak -echo for the terminal
* sncbr() Function to set -cbreak echo for the terminal
* newgame() Subroutine to save the initial time and seed rnd()
*
* FILE OUTPUT ROUTINES
*
* lprintf(format,args . . .) printf to the output buffer
* lprint(integer) send binary integer to output buffer
* lwrite(buf,len) write a buffer to the output buffer
* lprcat(str) sent string to output buffer
*
* FILE OUTPUT MACROS (in header.h)
*
* lprc(character) put the character into the output buffer
*
* FILE INPUT ROUTINES
*
* long lgetc() read one character from input buffer
* long lrint() read one integer from input buffer
* lrfill(address,number) put input bytes into a buffer
* char *lgetw() get a whitespace ended word from input
* char *lgetl() get a \n or EOF ended line from input
*
* FILE OPEN / CLOSE ROUTINES
*
* lcreat(filename) create a new file for write
* lopen(filename) open a file for read
* lappend(filename) open for append to an existing file
* lrclose() close the input file
* lwclose() close output file
* lflush() flush the output buffer
*
* Other Routines
*
* cursor(x,y) position cursor at [x,y]
* cursors() position cursor at [1,24] (saves memory)
* cl_line(x,y) Clear line at [1,y] and leave cursor at [x,y]
* cl_up(x,y) Clear screen from [x,1] to current line.
* cl_dn(x,y) Clear screen from [1,y] to end of display.
* standout(str) Print the string in standout mode.
* set_score_output() Called when output should be literally printed.
** putchar(ch) Print one character in decoded output buffer.
** flush_buf() Flush buffer with decoded output.
** init_term() Terminal initialization -- setup termcap info
** char *tmcapcnv(sd,ss) Routine to convert VT100 \33's to termcap format
* beep() Routine to emit a beep if enabled (see no-beep in .larnopts)
*
* Note: ** entries are available only in termcap mode.
*/
#include "header.h"
#ifdef SYSV /* system III or system V */
# ifdef UNIX
# include <termio.h>
# endif
#define sgttyb termio
#define stty(_a,_b) ioctl(_a,TCSETA,_b)
#define gtty(_a,_b) ioctl(_a,TCGETA,_b)
static int rawflg = 0;
static char saveeof,saveeol;
#define doraw(_a) if(!rawflg){++rawflg;saveeof=_a.c_cc[VMIN];saveeol=_a.c_cc[VTIME];}\
_a.c_cc[VMIN]=1;_a.c_cc[VTIME]=1;_a.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL)
#define unraw(_a) _a.c_cc[VMIN]=saveeof;_a.c_cc[VTIME]=saveeol;_a.c_lflag |= ICANON|ECHO|ECHOE|ECHOK|ECHONL
#else not SYSV
#ifndef BSD
#define CBREAK RAW /* V7 has no CBREAK */
#endif
#define doraw(_a) (_a.sg_flags |= CBREAK,_a.sg_flags &= ~ECHO)
#define unraw(_a) (_a.sg_flags &= ~CBREAK,_a.sg_flags |= ECHO)
#include <sgtty.h>
#endif not SYSV
#ifndef NOVARARGS /* if we have varargs */
#include <varargs.h>
#else NOVARARGS /* if we don't have varargs */
typedef char *va_list;
#define va_dcl int va_alist;
#define va_start(plist) plist = (char *) &va_alist
#define va_end(plist)
#define va_arg(plist,mode) ((mode *)(plist += sizeof(mode)))[-1]
#endif NOVARARGS
#define LINBUFSIZE 128 /* size of the lgetw() and lgetl() buffer */
int lfd; /* output file numbers */
int fd; /* input file numbers */
# ifdef UNIX
static struct sgttyb ttx; /* storage for the tty modes */
# endif
static int ipoint=MAXIBUF,iepoint=MAXIBUF; /* input buffering pointers */
static char lgetwbuf[LINBUFSIZE]; /* get line (word) buffer */
# ifndef UNIX
# ifdef MSDOS
# include <fcntl.h> /* For O_BINARY */
# endif
# ifdef GEMDOS
# include <osbind.h>
# include <xbios.h>
# endif
int (*getchfn)(), getche(), kgetch();
# endif
/*
* setupvt100() Subroutine to set up terminal in correct mode for game
*
* Attributes off, clear screen, set scrolling region, set tty mode
*/
setupvt100()
{
lprc(T_INIT);
clear(); setscroll(); scbr(); /* system("stty cbreak -echo"); */
#ifdef MSDOS
# ifdef DGK
setraw();
setcursor();
/* Select normal ASCII and line drawing character sets.
*/
if (DECRainbow)
lprcat("\033(B\033)0");
# endif
#endif
#ifdef GEMDOS
init_keys();
#endif
}
/*
* clearvt100() Subroutine to clean up terminal when the game is over
*
* Attributes off, clear screen, unset scrolling region, restore tty mode
*/
clearvt100()
{
lprc(T_END);
resetscroll(); clear(); sncbr(); /* system("stty -cbreak echo"); */
#ifdef MSDOS
# ifdef DGK
unsetraw();
resetcursor();
# endif
#endif
#ifdef GEMDOS
Bioskeys();
#endif
}
/*
* getchar() Routine to read in one character from the terminal
*/
getchar()
{
char byt;
#ifdef EXTRA
c[BYTESIN]++;
#endif
lflush(); /* be sure output buffer is flushed */
# ifndef UNIX
if ((byt = (*getchfn)()) == '\r')
byt = '\n';
return byt;
# else
read(0,&byt,1); /* get byte from terminal */
return(byt);
# endif
}
/*
* scbr() Function to set cbreak -echo for the terminal
*
* like: system("stty cbreak -echo")
*/
scbr()
{
# ifndef UNIX
/* Set up to use the direct console input call which may
* read from the keypad;
*/
getchfn = kgetch;
# else
gtty(0,&ttx); doraw(ttx); stty(0,&ttx);
# endif
}
/*
* sncbr() Function to set -cbreak echo for the terminal
*
* like: system("stty -cbreak echo")
*/
sncbr()
{
# ifndef UNIX
/* Set up to use the direct console input call with echo, getche()
*/
getchfn = getche;
# else
gtty(0,&ttx); unraw(ttx); stty(0,&ttx);
# endif
}
/*
* newgame() Subroutine to save the initial time and seed rnd()
*/
newgame()
{
register long *p,*pe;
for (p=c,pe=c+100; p<pe; *p++ =0);
time(&initialtime); srand(initialtime);
lcreat((char*)0); /* open buffering for output to terminal */
}
/*
* lprintf(format,args . . .) printf to the output buffer
* char *format;
* ??? args . . .
*
* Enter with the format string in "format", as per printf() usage
* and any needed arguments following it
* Note: lprintf() only supports %s, %c and %d, with width modifier and left
* or right justification.
* No correct checking for output buffer overflow is done, but flushes
* are done beforehand if needed.
* Returns nothing of value.
*/
#ifdef lint
/*VARARGS*/
lprintf(str)
char *str;
{
char *str2;
str2 = str;
str = str2; /* to make lint happy */
}
/*VARARGS*/
sprintf(str)
char *str;
{
char *str2;
str2 = str;
str = str2; /* to make lint happy */
}
#else lint
/*VARARGS*/
lprintf(va_alist)
va_dcl
{
va_list ap; /* pointer for variable argument list */
register char *fmt;
register char *outb,*tmpb;
register long wide,left,cont,n; /* data for lprintf */
char db[12]; /* %d buffer in lprintf */
va_start(ap); /* initialize the var args pointer */
fmt = va_arg(ap, char *); /* pointer to format string */
if (lpnt >= lpend) lflush();
outb = lpnt;
for ( ; ; )
{
while (*fmt != '%')
if (*fmt) *outb++ = *fmt++; else { lpnt=outb; return; }
wide = 0; left = 1; cont=1;
while (cont)
switch(*(++fmt))
{
case 'd': n = va_arg(ap, long);
if (n<0) { n = -n; *outb++ = '-'; if (wide) --wide; }
tmpb = db+11; *tmpb = (char)(n % 10 + '0');
while (n>9) *(--tmpb) = (char)((n /= 10) % 10 + '0');
if (wide==0) while (tmpb < db+12) *outb++ = *tmpb++;
else
{
wide -= db-tmpb+12;
if (left) while (wide-- > 0) *outb++ = ' ';
while (tmpb < db+12) *outb++ = *tmpb++;
if (left==0) while (wide-- > 0) *outb++ = ' ';
}
cont=0; break;
case 's': tmpb = va_arg(ap, char *);
if (wide==0) { while (*outb++ = *tmpb++); --outb; }
else
{
n = wide - strlen(tmpb);
if (left) while (n-- >